/*
 * Duplicate Detector Copyright (C) 2010 Marco Biscaro <marcobiscaro2112@gmail.com>
 * 
 * This file is part of Duplicate Detector.
 *
 * Duplicate Detector is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Duplicate Detector is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Duplicate Detector.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.duplicatedetector.view;

import static com.googlecode.duplicatedetector.i18n.Messages._;

import java.io.File;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import com.googlecode.duplicatedetector.Constants;
import com.googlecode.duplicatedetector.controller.DeleteFileActionListener;
import com.googlecode.duplicatedetector.controller.OpenDirActionListener;
import com.googlecode.duplicatedetector.controller.OpenFileActionListener;
import com.googlecode.duplicatedetector.i18n.Keys;
import com.googlecode.duplicatedetector.model.FileTreeModel;
import com.googlecode.duplicatedetector.util.FileUtils;

/**
 * Shows the file details.
 * 
 * @author Marco Biscaro
 */
public class FileDetailsPanel extends JPanel implements Constants, Keys {

	private static final long serialVersionUID = 8548383783657099482L;
	private File file;
	private JLabel fileNameLabel;
	private JLabel dirLabel;
	private JLabel readLabel;
	private JLabel writeLabel;
	private JLabel executeLabel;
	private JLabel hiddenLabel;
	private JLabel sizeLabel;
	private JLabel lastModifiedLabel;
	private JButton openFileButton;
	private JButton openDirButton;
	private JButton deleteButton;
	private transient FileTreeModel fileTreeModel;

	public FileDetailsPanel(FileTreeModel fileTreeModel) {
		this(null, fileTreeModel);
	}

	public FileDetailsPanel(File file, FileTreeModel fileTreeModel) {
		this.fileTreeModel = fileTreeModel;
		initialize();
		setFile(file);
	}

	private void initialize() {
		setLayout(new MigLayout("wrap 2", "[][grow]", "grow"));
		add(new JLabel(_(FILE_COLON)));
		add(getFileNameLabel());
		add(new JLabel(_(DIR)));
		add(getDirLabel());
		add(new JLabel(_(READ)));
		add(getReadLabel());
		add(new JLabel(_(WRITE)));
		add(getWriteLabel());
		add(new JLabel(_(EXECUTE)));
		add(getExecuteLabel());
		add(new JLabel(_(HIDDEN)));
		add(getHiddenLabel());
		add(new JLabel(_(SIZE)));
		add(getSizeLabel());
		add(new JLabel(_(LAST_MODIFIED)));
		add(getLastModifiedLabel());
		add(getOpenFileButton(), "w :100:, split 3, span 2");
		add(getOpenDirButton(), "w :100:");
		add(getDeleteButton(), "w :100:");
	}

	private JLabel getFileNameLabel() {
		if (fileNameLabel == null) {
			fileNameLabel = new JLabel();
			fileNameLabel.setText(UNKNOWN);
		}
		return fileNameLabel;
	}

	private JLabel getDirLabel() {
		if (dirLabel == null) {
			dirLabel = new JLabel();
			dirLabel.setText(UNKNOWN);
		}
		return dirLabel;
	}

	private JLabel getReadLabel() {
		if (readLabel == null) {
			readLabel = new JLabel();
			readLabel.setText(UNKNOWN);
		}
		return readLabel;
	}

	private JLabel getWriteLabel() {
		if (writeLabel == null) {
			writeLabel = new JLabel();
			writeLabel.setText(UNKNOWN);
		}
		return writeLabel;
	}

	private JLabel getExecuteLabel() {
		if (executeLabel == null) {
			executeLabel = new JLabel();
			executeLabel.setText(UNKNOWN);
		}
		return executeLabel;
	}

	private JLabel getHiddenLabel() {
		if (hiddenLabel == null) {
			hiddenLabel = new JLabel();
			hiddenLabel.setText(UNKNOWN);
		}
		return hiddenLabel;
	}

	private JLabel getSizeLabel() {
		if (sizeLabel == null) {
			sizeLabel = new JLabel();
			sizeLabel.setText(UNKNOWN);
		}
		return sizeLabel;
	}

	private JLabel getLastModifiedLabel() {
		if (lastModifiedLabel == null) {
			lastModifiedLabel = new JLabel();
			lastModifiedLabel.setText(UNKNOWN);
		}
		return lastModifiedLabel;
	}

	private JButton getOpenFileButton() {
		if (openFileButton == null) {
			openFileButton = new JButton();
			openFileButton.setText(_(OPEN));
			openFileButton.addActionListener(new OpenFileActionListener(this));
		}
		return openFileButton;
	}

	private JButton getOpenDirButton() {
		if (openDirButton == null) {
			openDirButton = new JButton();
			openDirButton.setText(_(OPEN_DIR));
			openDirButton.addActionListener(new OpenDirActionListener(this));
		}
		return openDirButton;
	}

	private JButton getDeleteButton() {
		if (deleteButton == null) {
			deleteButton = new JButton();
			deleteButton.setText(_(DELETE));
			deleteButton.addActionListener(new DeleteFileActionListener(this,
					fileTreeModel));
		}
		return deleteButton;
	}

	private void updateInfo() {
		String fileName = file == null ? UNKNOWN : file.getName();
		String dir = file == null ? UNKNOWN : file.getParent();
		String read = file == null ? UNKNOWN : file.canRead() ? _(YES) : _(NO);
		String write = file == null ? UNKNOWN : file.canWrite() ? _(YES)
				: _(NO);
		String exec = file == null ? UNKNOWN : file.canExecute() ? _(YES)
				: _(NO);
		String hidden = file == null ? UNKNOWN : file.isHidden() ? _(YES)
				: _(NO);
		String size = file == null ? UNKNOWN : FileUtils.getLength(file);
		String lastModified = file == null ? UNKNOWN : FileUtils
				.getLastModified(file);
		getFileNameLabel().setText(fileName);
		getDirLabel().setText(dir);
		getReadLabel().setText(read);
		getWriteLabel().setText(write);
		getExecuteLabel().setText(exec);
		getHiddenLabel().setText(hidden);
		getSizeLabel().setText(size);
		getLastModifiedLabel().setText(lastModified);
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
		updateInfo();
	}

}
